#include   <sys/types.h>
#include   <sys/socket.h>
#include   <string.h>
#include   <netinet/in.h>
#include   <stdio.h>
#include   <stdlib.h>
#include <errno.h>
#include   <arpa/inet.h>
#include   <unistd.h>
#include   <sys/time.h>
#include   <signal.h>

#define   MAXLINE   80
#define   SERV_PORT   8004
#define   PORT   8011

static int   ser_sockfd;
struct   sockaddr_in   servaddr;

void   handle   ()
{
	char   sendline[]= "0000000001_on_line\n ";
	int   ser_ret;


	ser_ret=sendto(ser_sockfd,   sendline,   sizeof(sendline),   0,   (struct   sockaddr   *)&servaddr,   sizeof(struct   sockaddr));

	if(ser_ret==-1)
	{
		perror( "send   error ");
		exit(1);
	}
}

void   init_sigaction()
{
	struct   sigaction   act;
	act.sa_handler=handle;
	act.sa_flags=0;
	sigemptyset(&act.sa_mask);
	sigaction(SIGALRM,&act,NULL);
}

void   init_time()
{
	struct   itimerval   value;
	value.it_value.tv_sec=2;
	value.it_value.tv_usec=0;
	value.it_interval=value.it_value;
	setitimer(ITIMER_REAL,&value,NULL);
}

int   main()
{
	char   recvmesg[MAXLINE];
	int   recvn;
	int   len;


	init_sigaction();
	init_time();

	/*   init   servaddr   */
	bzero(&servaddr,   sizeof(servaddr));
	servaddr.sin_family   =   AF_INET;
	servaddr.sin_port   =   htons(SERV_PORT);
	servaddr.sin_addr.s_addr   =   inet_addr("127.0.0.1");

	ser_sockfd=   socket(AF_INET,   SOCK_DGRAM,   0);
	if(connect(ser_sockfd,   (struct   sockaddr   *)&servaddr,   sizeof(struct   sockaddr))==   -1)
	{
		perror( "connect   error ");
		exit(1);
	}

	len   =   sizeof(struct   sockaddr_in);

	for(;;)
	{
		/*   waiting   for   receive   data   */
		memset(recvmesg,   0,   MAXLINE);
		recvn   =   recvfrom(ser_sockfd,   recvmesg,   MAXLINE,   0,   (struct   sockaddr   *)&servaddr,   &len);
		printf( "recvn=%s\n ",   recvmesg);
		if(recvn==-1)
		{
			if (errno == EINTR) {
				printf ("aaaaaaaaa\n");
				continue;
			}
			perror( "recv   error ");
			exit(1);
		}
		else
		{
			printf( "ser_recvn=%s\n ",   recvmesg);
		}
	}
	return   0;
} 
